# -*- coding: utf-8 -*-
import scrapy
from mini_scrapy.items import QuoteItem
import hashlib


class QuotesSpider(scrapy.Spider):
    name = 'quotes'
    allowed_domains = ['quotes.toscrape.com']
    start_urls = ['http://quotes.toscrape.com/']

    def get_md5(self, url):
        # str就是unicode了.Python3中的str对应2中的Unicode
        if isinstance(url, str):
            url = url.encode("utf-8")
        m = hashlib.md5()
        m.update(url)
        return m.hexdigest()

    def parse(self, response):
        quotes = response.css('.quote')
        for quote in quotes:
            item = QuoteItem()
            text = quote.css('.text::text').extract_first()
            author = quote.css('.author::text').extract_first()
            tags = quote.css('.tags .tag::text').extract()
            item['text'] = text
            item['author'] = author
            item['tags'] = tags
            item['url_object_id'] = self.get_md5(text)
            yield item

        next_page_url = response.css('.pager .next a::attr(href)').extract_first()
        if next_page_url:
            url = response.urljoin(next_page_url)
            yield scrapy.Request(url=url, callback=self.parse)
